package com.psp.bu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.northend.util.StringUtil;
import com.psp.bu.cache.dao.AreaCache;
import com.psp.bu.controller.res.bean.RAreaListBean;
import com.psp.bu.model.AreaBean;
import com.psp.bu.persist.dao.AreaDAO;
import com.psp.bu.service.AddressService;

@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	AreaDAO areaImpl;

	@Autowired
	AreaCache areaCacheImpl;

	/**
	 * 获取 根据获得子区域 当id 为空的时候，返回省级列表
	 */
	@Override
	public PageResult<RAreaListBean> getAreas(String id) {
		PageResult<RAreaListBean> result = new PageResult<>();
		List<RAreaListBean> resData = new ArrayList<>();

		if (StringUtil.isEmpty(id)) {
			id = "province";
		}

		List<AreaBean> reslist;
		String res = areaCacheImpl.getSubAreaById(id);
		if (!StringUtil.isEmpty(res)) {
			reslist = JSON.parseArray(res, AreaBean.class);
		} else {
			AreaBean areaBean = areaImpl.selectById(id);
			if (areaBean != null) {
				reslist = areaImpl.selectListByPid(areaBean.getId());
			} else {
				reslist = areaImpl.selectListByPid("");
			}
			Object resObj = JSON.toJSON(reslist);
			if (resObj != null) {
				areaCacheImpl.setSubAreaById(id, resObj.toString());
			}
		}

		int count = 0;
		if (reslist != null && reslist.size() > 0) {
			for (AreaBean bean : reslist) {
				RAreaListBean rb = new RAreaListBean();
				rb.setLabel(bean.getName());
				rb.setValue(bean.getId());
				resData.add(rb);
			}
			count = reslist.size();
		}
		result.setData(resData);
		result.setCount(count);
		return result;
	}

	@Override
	public PageResult<RAreaListBean> getAreas() {
		PageResult<RAreaListBean> result = new PageResult<>();

		String areapcd = areaCacheImpl.getAreaPCD();
		List<RAreaListBean> resultList;
		if (!StringUtil.isEmpty(areapcd)) {
			resultList = JSON.parseArray(areapcd, RAreaListBean.class);
		} else {
			List<AreaBean> lists = areaImpl.selectListByPCD();
			resultList = new ArrayList<>();
			if (lists != null && lists.size() > 0) {
				for (AreaBean pArea : lists) {
					if (pArea.getSubArea() != null && pArea.getSubArea().size() > 0) {
						RAreaListBean pAreaList = new RAreaListBean();
						pAreaList.setLabel(pArea.getName());
						pAreaList.setValue(pArea.getId());
						List<RAreaListBean> pSubList = new ArrayList<>();
						for (AreaBean cArea : pArea.getSubArea()) {
							RAreaListBean cArealist = new RAreaListBean();
							cArealist.setLabel(cArea.getName());
							cArealist.setValue(cArea.getId());
							pSubList.add(cArealist);
							if (cArea.getSubArea() != null && cArea.getSubArea().size() > 0) {
								List<RAreaListBean> cSubList = new ArrayList<>();
								cArealist.setChildren(cSubList);
								for (AreaBean dArea : cArea.getSubArea()) {
									RAreaListBean dAreaList = new RAreaListBean();
									dAreaList.setLabel(dArea.getName());
									dAreaList.setValue(dArea.getId());
									cSubList.add(dAreaList);
								}
							}
						}
						pAreaList.setChildren(pSubList);
						resultList.add(pAreaList);
					}
				}
				areaCacheImpl.setAreaPCD(JSON.toJSON(resultList).toString());
			}
		}

		result.setData(resultList);
		return result;
	}
}
